package study.core.broadcast

import org.apache.spark.{SparkConf, SparkContext}

/**
 * 广播变量
 *
 * @author zh
 * @date 2021/5/18 14:58
 */
object TestBroadCast {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local").setAppName("spark/core/broadcast")
    val sc = new SparkContext(sparkConf)

    val rdd = sc.makeRDD(List(("a", 1), ("b", 2), ("c", 3), ("d", 4)))

    val list = List(("a", 4), ("b", 3), ("c", 2), ("d", 1))

    // 声明广播变量
    val bc = sc.broadcast(list)

    val mapRdd = rdd.map(t => {
      val bcv = bc.value
      var num = 0
      t match {
        case (key, value) => {
          for ((k, v) <- bcv) {
            if (k == key) {
              num = v
            }
          }
        }
      }
      (t._1, t._2 + num)


    })
    mapRdd.collect().foreach(println)

  }
}
